﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using Maticsoft.Common;

namespace Maticsoft.Web.Admin.Shopping
{
    public partial class EditBookComment : PageBase
    {
        string act = string.Empty;
        string id = string.Empty;
        string bookId = string.Empty;

        BLL.BookComments bll = new BLL.BookComments();
        Model.BookComments model = null;
        protected void Page_Load(object sender, EventArgs e)
        {
            Check("shopping", "modify");

            if (!string.IsNullOrEmpty(Request.QueryString["act"]))
            {
                act = Request.QueryString["act"];
            }
            if (!string.IsNullOrEmpty(Request.QueryString["id"]))
            {
                id = Request.QueryString["id"];
            }
            if (!string.IsNullOrEmpty(Request.QueryString["bookId"]))
            {
                bookId = Request.QueryString["bookId"];
            }

            if (!IsPostBack)
            {
                GetData();
            }
        }

        protected void GetData()
        {
            BLL.Books bookBll = new BLL.Books();
            Model.Books bookMobel=null;
            if (!string.IsNullOrEmpty(bookId))
            {
                bookMobel = bookBll.GetModel(int.Parse(bookId));
                txtBookId.Text = bookId;
                txtBookName.Text = bookMobel.BookName;
            }

            if (!string.IsNullOrEmpty(id))
            {
                model = bll.GetModel(int.Parse(id));
                txtTitle.Text = model.Title;
                txtAuthor.Text = model.Author;
                FCKeditor1.Value = model.Detail;
                cbTop.Checked = model.IsTop == 1 ? true : false;
                
                bookMobel = bookBll.GetModel(int.Parse(model.BookId.ToString()));
                txtBookId.Text = bookMobel.BookId.ToString();
                txtBookName.Text = bookMobel.BookName;
            }
        }

        protected void ImageButton1_Click(object sender, ImageClickEventArgs e)
        {
            if (act == "add")
            {
                model = new Model.BookComments();
                model.BookId = int.Parse(txtBookId.Text.Trim());
                model.Title = txtTitle.Text.Trim();
                model.Author = txtAuthor.Text.Trim();
                model.Detail = FCKeditor1.Value;
                model.IsActive = 1;
                model.CreateTime = DateTime.Now;
                model.UpdateTime = DateTime.Now;
                model.IsTop = cbTop.Checked == true ? 1 : 0;

                bll.Add(model);

                MessageBox.ShowAndRedirect(this.Page, "添加成功", "BookCommentList.aspx");
            }
            else if (act == "edit" && !string.IsNullOrEmpty(id))
            {
                model = bll.GetModel(int.Parse(id));
                model.BookId = int.Parse(txtBookId.Text.Trim());
                model.Title = txtTitle.Text.Trim();
                model.Author = txtAuthor.Text.Trim();
                model.Detail = FCKeditor1.Value;
                model.IsActive = 1;
                model.UpdateTime = DateTime.Now;
                model.IsTop = cbTop.Checked == true ? 1 : 0;

                bll.Update(model);

                MessageBox.ShowAndRedirect(this.Page, "修改成功", "BookCommentList.aspx");
            }
        }
    }
}